package com.heima.freemarker.controller;

import com.heima.freemarker.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 需求：
 * 从后台的某个方法中去跳转页面到01-basic.ftl文件中并渲染页面
 * springmvc中有三个类或者接口能够去封装数据给前台进行渲染
 * model接口
 * ModelMap
 * ModelAndView
 */
@Controller
@RequestMapping("/hello")
public class HelloController {
    /**
     * model接口没有见过
     * @param model
     * @return
     */
    @GetMapping
    public String hello(Model model){
        //1.封装数据
        model.addAttribute("name","张三");
        Student student=new Student();
        student.setName("二狗子");
        student.setAge(12);
        model.addAttribute("stu",student);
        //2.跳转页面
        return "01-basic";
    }

    /**
     * modelMap
     * @param model
     * @return
     */
    @GetMapping("/modelMap")
    public String hello(ModelMap model){
        //1.封装数据
        //model.addAttribute("name","张三");
        model.put("name","张三222");
        Student student=new Student();
        student.setName("二狗子");
        student.setAge(12);
        model.addAttribute("stu",student);
        //2.跳转页面
        return "01-basic";
    }

    /**
     * modelAndView模型与视图的
     * @param
     * @return
     */
    @GetMapping("/mav")
    public ModelAndView hello(){
        ModelAndView mav=new ModelAndView();
        //1.封装数据
        mav.addObject("name","张三");

        Student student=new Student();
        student.setName("二狗子");
        student.setAge(12);
        mav.addObject("stu",student);
        //2.跳转页面
        mav.setViewName("01-basic");
        return mav;
    }

    @GetMapping("/list")
    public String list(ModelMap modelMap){
        //封装数据
        Student student=new Student();
        student.setName("二狗子");
        student.setAge(12);
        student.setMoney(50d);

        Student student2=new Student();
        student2.setName("三狗子");
        student2.setAge(24);
        student2.setMoney(100d);

        List list=new ArrayList();
        list.add(student);
        list.add(student2);

        modelMap.put("studentList",list);

        //map封装
        Map resultMap=new HashMap<>();
        resultMap.put("stu1",student);
        resultMap.put("stu2",student2);

        modelMap.put("resultMap",resultMap);



        return "02-list";
    }
}
